﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Threading;
using System.Text.RegularExpressions;
namespace 橘子树chat
{
    /// <summary>
    /// validation.xaml 的交互逻辑
    /// </summary>
    public partial class validation : Window
    {
        [DllImport("maindll.dll", EntryPoint = "yan", CallingConvention = CallingConvention.Cdecl)]
        public static extern String yan(String address);
        public validation()
        {
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            InitializeComponent();
        }

        private void label3_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DialogResult = false;
            this.Close();
        }
        private System.Windows.Threading.DispatcherTimer dt = new DispatcherTimer();
        void dt_Tick(object sender, EventArgs e)
        {
            // 要执行的任务
            //toast_MissingInformation.Visibility = Visibility.Hidden;
            label4.Visibility = Visibility.Hidden;
            dt.Stop();
        }
        int free = 0;
        private System.Windows.Threading.DispatcherTimer dt2 = new DispatcherTimer();
        void dt2_Tick(object sender, EventArgs e)
        {
            // 要执行的任务
            //toast_MissingInformation.Visibility = Visibility.Hidden;
            free -= 1;
            button.Content = "提交("+free+"s)";
            if (free <= 0)
            {
                button.Content = "提交";
                button.IsEnabled = true;
                dt2.Stop();
            }
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            Regex re = new Regex(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
            if (re.IsMatch(textBox.Text))
            {
               
                MessageBox.Show("准备");
                String result="";
                yan(textBox.Text);
                MessageBox.Show("完毕");
                free = 30;
                button.IsEnabled = false;
                dt2.Interval = TimeSpan.FromMilliseconds(1000);
                dt2.Tick += new EventHandler(dt2_Tick);
                dt2.Start();
                MessageBox.Show(result);
                if (result!= "faild" && result!="")
                {
                    MessageBox.Show("通过");
                    InputVali vali = new InputVali(result);
                    this.Hide();
                    MessageBox.Show("准备显示第二对话框");
                    bool? SecondResult=vali.ShowDialog();
                    if (SecondResult == true)
                    {
                        this.DialogResult = true;
                        this.Close();
                    }
                
                }
                else
                {
                    MessageBox.Show("失败");
                    label4.Visibility = Visibility.Visible;
                    dt.Interval = TimeSpan.FromMilliseconds(2500);
                    dt.Tick += new EventHandler(dt_Tick);
                    dt.Start();
                }
            }
            else
            {
                label4.Visibility = Visibility.Visible;

                dt.Interval = TimeSpan.FromMilliseconds(2500);
                dt.Tick += new EventHandler(dt_Tick);
                dt.Start();
            }
            
        }
    }
}
